const Router = require('koa-joi-router')
const config = require('../config')
const swagger = require('../config/swagger')
const {koaSwagger} = require('koa2-swagger-ui')

module.exports = (opts, app) => {
  if (config.env !== 'production') {
    const router = Router()
    router.get(opts.swaggerOptions.url, async ctx => {
      ctx.body = swagger
    })
    app.use(router.middleware())
    return koaSwagger(opts)
  }

  return (ctx, next) => next()
}